﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL.Controller;
using EntityObjects;

namespace PresentationLayer.View.DepartmentEmployee
{
    public partial class StationeryRequisitionForm : System.Web.UI.Page
    {
        private static MakeRequisitionControl makeRequisitionControl=new MakeRequisitionControl();
        private static Requisition req;
        private static Employee emp;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                emp = (Employee)Session["CurrentUser"];
                req = (Requisition)Session["Requisition"];

                //Check is it come from SelectStationeryItems.aspx
                if (req == null)
                {
                    req = MakeNewRequisition(emp);
                }
                //Check the login employee has already applied or not
                if (req != null)
                {
                    PanelData.Visible = true;
                    PopulateRequisitionData();
                }
                else
                {
                    PanelMsg.Visible = true;
                    lblmsg.Text = "You have already Applied and is not approved by Dept Head yet.\n So, now you can't make new requisition";
                }
            }
        }
                       
        protected void btnAddStationeryItems_Click(object sender, EventArgs e)
        {
            Session["Requisition"] = req;
            Response.Redirect("/View/DepartmentEmployee/SelectStationeryItems.aspx");
        }

        protected void ReqDetailsView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = ReqDetailsView.Rows[e.RowIndex];            
            int updateReqQty = Convert.ToInt32(((TextBox)row.Cells[3].FindControl("tbQty")).Text);
            req.ReqDetails_list[e.RowIndex].RequestedQty = updateReqQty;           
            
            PopulateRequisitionDetailsData();            
        }

        protected void ReqDetailsView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            req.ReqDetails_list.RemoveAt(e.RowIndex);
            PopulateRequisitionData();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //Call Controller object and methods 
            lblmsg.Text = makeRequisitionControl.selectSubmit(req,emp);

            lblmsg.Visible = true;
            PanelMsg.Visible = true;
            PanelData.Visible = false;
            Session.Remove("Requisition");           
        }
                         

        private Requisition MakeNewRequisition(Employee emp)
        {
            Requisition newReq = makeRequisitionControl.makeRequisition(emp);
            return newReq;
        }
        
        private void PopulateRequisitionData()
        {                                    
            lblEmployeeName.Text = req.EmpName;
            lblEmployeeNumber.Text = req.EmpId;
            lblDepartmentCode.Text = req.DeptId;
            lblEmail.Text = emp.Email;
            lblAppliedDate.Text = DateTime.Parse(req.RequestedDate).ToShortDateString();
            lblStatus.Text = req.Status;

            PopulateRequisitionDetailsData();
        }
       
        private void PopulateRequisitionDetailsData()
        {
            if (req.ReqDetails_list.Count > 0)
            {
                ReqDetailsView.DataSource = req.ReqDetails_list;
                ReqDetailsView.DataBind();
                btnSubmit.Visible = true;                
            }
            else
            {
                ReqDetailsView.DataSource = null;
                ReqDetailsView.DataBind();
                btnSubmit.Visible = false;                
            }
        }
    }
}